import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import javax.swing.*;

public class LifeGameView extends JFrame implements Observer {

	private JMenuBar _menubar;
	private JPanel _mainPanel;
	private JPanel _gamePanel;
	private JPanel _actionPanel;
	private LinkedList<JButton> _actionButtons;
	private LifeGameControler _controler;
	private LifeGameModel _model;
	private JSlider _widhtSlider;
	private JSlider _heigthSlider;
	private JButton _btnSave;
	private JButton _btnLoad;
	private JCheckBox _chkboxLifeRule;
	private JSlider _sldTimelaps;
	private JSlider _sldLife;

	public LifeGameView( LifeGameControler controler, LifeGameModel model ) {
		
		UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
				
		try {
			   UIManager.setLookAndFeel(looks[3].getClassName());
			   SwingUtilities.updateComponentTreeUI(this);
			} catch (InstantiationException e) {
			} catch (ClassNotFoundException e) {
			} catch (UnsupportedLookAndFeelException e) {
			} catch (IllegalAccessException e) {}
		
		this.setTitle("Jeu de la vie");
		this.setSize(640, 480);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

		_controler = controler;
		_model = model;

		//Initialisation des contrôles
		_menubar = new JMenuBar();
		
		_mainPanel = new JPanel( true);
		_mainPanel.setLayout(new BorderLayout());
		_gamePanel = new JPanel(new BorderLayout(1,1), true);
		_gamePanel.setPreferredSize(new Dimension(500,500));
		_actionPanel = new JPanel(new BorderLayout(1,1), true);
		_actionPanel.setLayout(new GridLayout(0,1));
		_actionPanel.setMaximumSize(new Dimension(100, 100));
		_actionPanel.setMinimumSize(new Dimension(100, 100));
		_actionButtons = new LinkedList<JButton>();
		_heigthSlider = new JSlider(10,100,_model.get_heigth());
		_widhtSlider = new JSlider(10,100,_model.get_widht());
		_btnSave = new JButton("Enregistrer");
		_btnLoad = new JButton("Charger");
		_chkboxLifeRule = new JCheckBox("Utiliser la vie");
		_sldTimelaps = new JSlider(10,1000);
		_sldLife = new JSlider(1,100);
		
		//Structuration de l'interface
		_actionButtons.add(new JButton("Génèse"));
		_actionButtons.add(new JButton("Réinitialiser"));
		_actionButtons.add(new JButton("Mettre en pause"));
		_actionButtons.add(_btnSave);
		_actionButtons.add(_btnLoad);
		
		_controler.setGenesisButton(_actionButtons.get(0));
		_controler.setResetButton(_actionButtons.get(1));
		_controler.setPauseButton(_actionButtons.get(2));
		_controler.setSliders(_widhtSlider, _heigthSlider);
		_controler.setGamePanel(_gamePanel);
		_controler.setSaveButton(_btnSave);
		_controler.setLoadButton(_btnLoad);
		_controler.setLifeRuleCheckBox(_chkboxLifeRule);
		_controler.setTimelapsSlider(_sldTimelaps);
		_controler.setLifeSlider(_sldLife);
		
		for(int i = 0; i < _actionButtons.size(); i++)
		{
			_actionPanel.add(_actionButtons.get(i));
		}
		
		_actionPanel.add(_chkboxLifeRule);
		_actionPanel.add(new JLabel("Hauteur"));
		_actionPanel.add(_heigthSlider);
		_actionPanel.add(new JLabel("Largeur"));
		_actionPanel.add(_widhtSlider);
		_actionPanel.add(new JLabel("Vitesse"));
		_actionPanel.add(_sldTimelaps);
		_actionPanel.add(new JLabel("Nombre de vie"));
		_actionPanel.add(_sldLife);
		
		_controler.createCells();

		_mainPanel.add(_gamePanel,BorderLayout.CENTER);
		_mainPanel.add(_actionPanel,BorderLayout.LINE_END);

		this.add(_menubar);
		this.add(_mainPanel);

		this.setVisible(true);
		_model.addObserver(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		_controler.updateCells();
	}

}
